package com.thx.common.orm;

import com.thx.common.orm.model.Template;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 模版 Dao.
 */
@Repository
public class TemplateDao extends PaginationDao<Template> {

  /**
   * 根据租户id和实体类名称查询启用的模板（saas模式）.
   */
  public List<Template> findByTenantIdAndEntityClass(String tenantId, String entityClassName) {
    String hql = "from Template where tenantId=? and entityClass=? and state = '启用'";
    Query query = createQuery(hql, new Object[] {tenantId, entityClassName});
    List<Template> list = query.list();
    return list;
  }


  /**
   * 根据实体类名称查询启用的模板（传统模式）.
   */
  public List<Template> findByEntityClass(String entityClassName) {
    String hql = "from Template where entityClass=? and state = '启用'";
    Query query = createQuery(hql, entityClassName);
    List<Template> list = query.list();
    return list;
  }

  /**
   * 按租户 id 查询模版.
   */
  public List<Template> findByTenantId(String tenantId) {
    String hql = "from Template where 1=1";
    List<String> params = new ArrayList<String>();
    if (StringUtils.isNotBlank(tenantId)) {
      hql += " and tenantId=? ";
      params.add(tenantId);
    }
    return createQuery(hql, params.toArray()).list();
  }

}
